/*
 * 文件名：BudgetBatchController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月17日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.BudgetListDto;
import top.infopub.dto.bpa.BudgetListVo;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.dao.BudgetBatchDao;
import top.infopub.mgr.bpa.jsonVo.BudgetBatchDetailJsonVo;
import top.infopub.mgr.bpa.service.*;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.service.ProjectInfoService;
import top.infopub.model.bpa.BudgetBatch;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;


/**商务事项
 * @author wuzhiyang
 * @version 2020年3月17日
 * @see BudgetBatchController
 * @since商务管理/商务事项/概算分批
 */
@Controller
@RequestMapping("/budgetBatch")
public class BudgetBatchController extends GenericFlowController {

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private BudgetBatchService budgetBatchService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private ProContractDetailService proContractDetailService;

    @Autowired
    private ProContractSubService proContractSubService;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Autowired
    private BudgetBatchDao budgetBatchDao;

    @Autowired
    private ProjectInfoService projectInfoService;

    /**
     *概算分批列表
     * @return 
     * @see
     */
    @RequestMapping("/list")
    public String list() {
        return "bpa/lay-budgetBatch-list";
    }

    /**
     * 概算分批列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BudgetListDto> queryDataByCondition(BudgetListVo budgetListVo,
                                                         ParamPage paramPage) {
        Page<BudgetListDto> page = new Page<BudgetListDto>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());

        page = budgetBatchService.selectByDomainWhere(page, budgetListVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    //根据procode获取项目的基本信息和合同列表
    @RequestMapping("/queryProjAndContract")
    @ResponseBody
    public RspVo queryBudgetByProCode(String proCode) {
        //项目的基础信息
        BudgetBatchDetailJsonVo budgetBatchDetailJsonVo = budgetBatchService.getProInfoAndContract(proCode);
        return RspVo.getSuccessResponse(budgetBatchDetailJsonVo);
    }

    //根据合同id获取子项列表以及费用清单内容
    @RequestMapping("/queryBudgetInfoByContractId")
    @ResponseBody
    public RspVo queryBudgetInfoByContractId(String contractId) throws Exception {
        //项目的基础信息
        BudgetBatchDetailJsonVo budgetBatchDetailJsonVo = budgetBatchService.queryBudgetInfoByContractId(contractId);
        return RspVo.getSuccessResponse(budgetBatchDetailJsonVo);
    }

    /**
     * 概算分批-新增  0=保存，1=提交
     * @throws ParseException
     */
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, Integer submitType) {

        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();

        String str = StringEscapeUtils.unescapeJava(bodyStr);
        BudgetBatchDetailJsonVo detailJson = JSON.parseObject(str, BudgetBatchDetailJsonVo.class);
        return budgetBatchService.save(detailJson, submitType, vo);
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetail(String id) {
        BudgetBatchDetailJsonVo detailJson = budgetBatchService.getDetail(id);
        return RspVo.getSuccessResponse(detailJson);
    }

    /**
     * 根据id查询概算分批详细  > 注意：方法命名必须为：toDealOrDetail
     * @param id 概算分批id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request,String id, String proCode) {

        request.setAttribute("id", id);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审
        if (StringUtils.isNotBlank(id))  {
            BudgetBatch budgetBatch = budgetBatchDao.selectByPrimaryKey(id);
            if (budgetBatch != null) {
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), budgetBatch.getProcessStatus())
                        || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), budgetBatch.getProcessStatus())) {
                    request.setAttribute("isAuditFlag", "0");
                }
                if (StringUtils.isNotBlank(budgetBatch.getProcessId()) && budgetBatch.getProcessId().length() > 10) {
                    request.setAttribute("processId", budgetBatch.getProcessId());
                    String auditTabFlag = bpaProcessManageService.checkShowAuditTab(budgetBatch.getProcessId(), vo);
                    if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), budgetBatch.getProcessStatus())
                            && StringUtils.equals("0", auditTabFlag)) {
                        request.setAttribute("isAuditFlag", "0");
                    }
                    request.setAttribute("auditTabFlag", auditTabFlag);
                }
            }
        }
        request.setAttribute("proCode", proCode);

        return "bpa/lay-EstimateBatch-detail";
    }

}
